package com.musifind.activities;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.musifind.chat.ChatActivity;
import com.musifind.chat.Config;
import com.musifind.chat.ShareExternalServer;
import com.musifind.common.GeneralHelper;
import com.musifind.utils.ImageUtils;
import com.musifind.utils.SessionManager;
import com.musifind.wsclient.objects.Matching;
import com.musifind.wsclient.objects.User;
import com.musifind.wsclient.service.MatchingService;
import com.musifind.wsclient.service.UserService;

public class ConfirmMatchActivity extends Activity {

	private ImageView imgOpposite;
	private TextView txtOppositeName, txtTrack, txtArtist;
	private Button btnConfirm, btnReject;
	ShareExternalServer appUtil;
	SessionManager session;
	String fromRegId;
	String fromUserName;
	String trackId;
	String trackName;
	String artistName;
	String fromUserId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		StrictMode.setThreadPolicy(policy);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_confirm_match);
		appUtil = new ShareExternalServer();
		session = new SessionManager(getApplicationContext());

		imgOpposite = (ImageView) findViewById(R.id.imgOpposite);
		txtOppositeName = (TextView) findViewById(R.id.txtOppositeName);
		txtTrack = (TextView) findViewById(R.id.txtTrack);
		txtArtist = (TextView) findViewById(R.id.txtArtist);
		btnConfirm = (Button) findViewById(R.id.btnConfirm);
		btnReject = (Button) findViewById(R.id.btnReject);

		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		if (bundle != null) {
			fromRegId = (String) bundle.get(Config.FROM_REG_ID);
			fromUserId = (String) bundle.get(Config.FROM_USER_ID);
			// ekranda gosterilecek
			fromUserName = (String) bundle.get(Config.FROM_USER_NAME);
			trackId = bundle.getString(Config.TRACK_ID);
			trackName = (String) bundle.get(Config.TRACK_NAME);
			artistName = (String) bundle.get(Config.ARTIST_NAME);
		}

		UserService userService = new UserService();
		User user = userService.getUser(new Integer(fromUserId));
		if (user.getImage() != null && user.getImage().length > 0) {
			imgOpposite.setImageBitmap(GeneralHelper.roundCornerImage(ImageUtils.getBitmap(user.getImage()), 120));
		} else {
			Bitmap defaultIcon = GeneralHelper.drawableToBitmap(getResources().getDrawable(R.drawable.pp_facebook));
			imgOpposite.setImageBitmap(GeneralHelper.roundCornerImage(defaultIcon, 280));
		}

		txtOppositeName.setText(fromUserName);

		txtTrack.setText(trackName);
		txtArtist.setText(artistName);

		btnConfirm.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// cevap gonceriliyor
				MatchingService matchingService = new MatchingService();
				Matching matching = matchingService.createMatching(new Integer(fromUserId), session.getUserId());

				String matchingId = matching.getId() + "";
				sendMatchingResponseToGCMAppServer(Config.ACTION_MATCH_CONFIRM, fromRegId, session.getUsername(),
						trackId, String.valueOf(session.getUserId()), matchingId);

				Intent i = new Intent(ConfirmMatchActivity.this, ChatActivity.class);
				i.putExtra("trackId", trackId);
				i.putExtra("fromRegId", fromRegId);
				i.putExtra("fromUserId", fromUserId);
				i.putExtra("mId", matchingId);
				i.putExtra("action", "S");
				startActivity(i);
			}
		});

		btnReject.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				sendMatchingResponseToGCMAppServer(Config.ACTION_MATCH_REJECT, fromRegId, session.getUsername(), null,
						null, null);
				Intent i = new Intent(ConfirmMatchActivity.this, MusicPlayerActivity.class);
				startActivity(i);
			}
		});
	}

	private void sendMatchingResponseToGCMAppServer(final String action, final String toRegId, final String userName,
			final String trackId, final String fromUserId, final String matchingId) {
		new AsyncTask<Void, Void, String>() {
			@Override
			protected String doInBackground(Void... params) {
				String result = appUtil.sendMessage(action, session.getGCMRegistrationId(), toRegId, userName, trackId,
						trackName, artistName, fromUserId, String.valueOf(session.getUserId()), matchingId);
				Log.d("MainActivity", "matchingId: " + matchingId);
				Log.d("MainActivity", "Result: " + result);
				return result;
			}

			@Override
			protected void onPostExecute(String msg) {
				Log.d("MainActivity", "Result: " + msg);
				GeneralHelper.showPurpleToast(getApplicationContext(), msg);
			}
		}.execute(null, null, null);
	}
}
